package cate.game.play.buff.h;


import cate.game.attr.FightAttr;
import cate.game.play.buff.BuffHandler;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.SkillActionCtx;
import cate.game.play.skill.effect.SkillEffect;
import cate.game.play.util.FighterFilter;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

@NoteClass("临时属性变更")
public class 临时属性变更BH extends BuffHandler {

    @NoteField("配置")
    private List<Config> configs = new ArrayList<>();

    @Override
    public void parse(String code) {
        if(StringUtils.isNotBlank(code)){
            for (String s : code.split(",")) {
                configs.add(new Config(s));
            }
        }
    }

    public static class Config{

        @NoteField("配置")
        private PlayCfgArgs args;

        @NoteField("属性")
        private FightAttr 属性;

        @NoteField("施法者属性")
        private boolean 施法者属性;

        public Config(String code) {
            parse(code);
        }

        //属性=1:300&施法者属性=1
        public void parse(String code) {
            args = new PlayCfgArgs(code);
            属性 = new FightAttr(args.get("属性"));
            施法者属性 = args.getBoolean("施法者属性", true);
        }

        public void settle(SkillActionCtx action, SkillEffect effect, Fighter target, boolean addToActor) {
            if (addToActor != 施法者属性) {
                return;
            }
            if (FighterFilter.test(args, action.getActor(), target)) {
                if (addToActor) {
                    effect.getTargetCtx(target.getPid()).addActorAttr(this.属性);
                } else {
                    effect.getTargetCtx(target.getPid()).addTargetAttr(this.属性);
                }
            }
        }
    }

    @Override
    public void beforeSkillSettleAsActor(SkillActionCtx action, SkillEffect effect, Fighter target) {
        for (Config config : configs) {
            config.settle(action, effect, target,true);
        }
    }

    @Override
    public void beforeSkillSettleAsTarget(SkillActionCtx action, SkillEffect effect) {
        for (Config config : configs) {
            config.settle(action, effect, buff.owner,false);
        }
    }

    @Override
    public String toString() {
        return "临时属性变更";
    }
}
